package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.domain.StrategyCanal;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * @author 浪兎兎
 * @since 2025-06-16 15:37
 */
@Component
@CanalTable("ta_strategy")
@Slf4j
public class StrategyListener implements EntryHandler<StrategyCanal> {

    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private RedisService redisService;

    @Override
    public void insert(StrategyCanal strategyCanal) {
        log.info("添加攻略：{}", strategyCanal);
        // 同步es
        String message = JSON.toJSONString(strategyCanal);
        String key = RedisKeys.MQ_MESSAGE.join("strategy");
        redisService.addCacheZsetValue(key, message, System.currentTimeMillis());
        amqpTemplate.convertAndSend("strategyExchange", "", message);
    }

    @Override
    public void update(StrategyCanal before, StrategyCanal after) {
        log.info("修改攻略：{}", after);
        // 同步es
        String message = JSON.toJSONString(after);
        String key = RedisKeys.MQ_MESSAGE.join("strategy");
        redisService.addCacheZsetValue(key, message, System.currentTimeMillis());
        amqpTemplate.convertAndSend("strategyExchange", "", message);
    }

    @Override
    public void delete(StrategyCanal strategyCanal) {
        log.info("删除攻略：{}", strategyCanal);
    }
}
